package com.wunong.smart.commission.domain.state.operation.impl;

import com.wunong.smart.commission.application.context.CommissionContext;
import com.wunong.smart.commission.application.request.support.CommissionEntryRequest;
import com.wunong.smart.commission.application.request.support.CreateCommissionRequest;
import com.wunong.smart.commission.client.enums.CommissionOperationEnum;
import com.wunong.smart.commission.client.enums.CommissionStatusEnum;
import com.wunong.smart.commission.domain.business.CreateCommissionBusinessExt;
import com.wunong.smart.commission.domain.model.CommissionDomain;
import com.wunong.smart.commission.domain.state.operation.AbstractCommissionOperation;
import com.wunong.smart.domain.platform.common.VerifyDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author create by zealot.zt
 */
@Slf4j
@Component
public class CreateCommissionOperation extends AbstractCommissionOperation<CreateCommissionRequest> {

    @Resource
    private CreateCommissionBusinessExt createCommissionBusinessExt;

    @Override
    public VerifyDTO canOperate(CommissionContext<CreateCommissionRequest> context) {
        CommissionContext<CommissionEntryRequest> entryContext = CommissionContext.of(
                CommissionEntryRequest.of(context.getRequest()));

        return createCommissionBusinessExt.canCreate(entryContext);
    }

    @Override
    protected Object doOperate(CommissionContext<CreateCommissionRequest> context) {
        CommissionDomain domain = context.getModel();

        CreateCommissionRequest request = context.getRequest();

        domain.setUserId(request.getUserId());
        domain.setUserRole(request.getUserRole());
        domain.setSource(request.getSource());
        domain.setCommissionType(request.getCommissionType());
        domain.setStatusEnum(CommissionStatusEnum.PROCESSING);
        domain.setContent(request.getContent());

        Long id = commissionRepository.save(domain);
        log.warn("commissionOperation#create, success, user:{}, source:{}, id:{}",
                domain.getUser(), domain.getSource(), id);

        return domain;
    }

    @Override
    public CommissionOperationEnum getOperationEnum() {
        return CommissionOperationEnum.CREATE;
    }

}
